#!/usr/bin/env python
# encoding: utf-8
"""
#-------------------------------------------------------------------#
#                   CONFIDENTIAL --- CUSTOM STUDIOS                 #     
#-------------------------------------------------------------------#
#                                                                   #
#                   @Project Name : Globallawonline                #
#                                                                   #
#                   @File Name    : changfile.py                      #
#                                                                   #
#                   @Programmer   : 李建                            #
#                                                                   #  
#                   @Start Date   : 2021/6/11 9:44                 #
#                                                                   #
#                   @Last Update  : 2021/6/11 9:44                 #
#                                                                   #
#-------------------------------------------------------------------#
# Classes:修改加工库中与采集库中数据不一致字段                                                          #
#                                                                   #
#-------------------------------------------------------------------#
"""
import pymysql

from Seetings import JGMYSQL_CONNEXT, CJMYSQL_CONNECT

cj_conn = pymysql.connect(host=CJMYSQL_CONNECT["MYSQL_HOST"],
                              port=CJMYSQL_CONNECT["MYSQL_PORT"],
                              db=CJMYSQL_CONNECT["MYSQL_DB"],
                              user=CJMYSQL_CONNECT["MYSQL_USER"],
                              passwd=CJMYSQL_CONNECT["MYSQL_PASSWD"],
                              charset=CJMYSQL_CONNECT["MYSQL_CHARSET"])
cj_cursor = cj_conn.cursor()
filelist = ["CaseNumber"]
jg_filelist = {"CaseNumber": "CaseNumber", "AdjudicationDate": "AdjudicationDateStr", "Title": "Title"}
country = "LAWCOUNTRYWL"
table = "`lawcasetext`"
jg_table_dict = {"`lawcasetext`": "`case`"}
filelist_str = ",".join(filelist)
cj_filelist_strl = []
for file in filelist:
    file_str = '''%s IS NOT NULL AND %s != ""''' % (file, file)
    cj_filelist_strl.append(file_str)
cj_filelist_str = " and ".join(cj_filelist_strl)
sql = '''SELECT SYSID,%s FROM %s WHERE SortA = "%s" AND %s''' % (filelist_str, table, country, cj_filelist_str)
try:
    cj_cursor.execute(sql)
    results = cj_cursor.fetchall()
except Exception as e:
    print("采集数据库查询出错:%s" % str(e))
jg_conn = pymysql.connect(host=JGMYSQL_CONNEXT["MYSQL_HOST"],
                              port=JGMYSQL_CONNEXT["MYSQL_PORT"],
                              db=JGMYSQL_CONNEXT["MYSQL_DB"],
                              user=JGMYSQL_CONNEXT["MYSQL_USER"],
                              passwd=JGMYSQL_CONNEXT["MYSQL_PASSWD"],
                              charset=JGMYSQL_CONNEXT["MYSQL_CHARSET"])
jg_cursor = jg_conn.cursor()
jg_table = jg_table_dict[table]
j = 0
n = len(results)
for result in results:
    jg_filelist_strl = []
    for i in range(0, len(filelist)):
        file_str = '''%s = "%s"''' % (jg_filelist[filelist[i]], result[i+1])
        jg_filelist_strl.append(file_str)
    jg_filelist_str = ",".join(jg_filelist_strl)
    jgsql = '''update %s set %s where sysid = "%s"''' % (jg_table,jg_filelist_str, result[0])
    try:
        jg_cursor.execute(jgsql)
        jg_conn.commit()
    except Exception as e:
        with open("updateerro.txt", 'a+', encoding='utf-8') as f:
            f.write(result[0] + ";" + result[1] + '\n')
        print("加工数据库修改出错:%s" % str(e))
    j = j + 1
    print("\r共%d已处理: %d" % (n,j), end="")


